﻿using System;
using System.Collections;
using Yunchang;
using UnityEngine;


namespace Yunchang
{
    public class LoadShipEvent : LoadEventBase
    {
        private int modelId;
        private string animType;
        private string modelRes;
        private bool isMainModel;

        private string modelPath;
        private string modelAniPath;

        public LoadShipEvent(LoadEventManager manager, int modelId, string animType) : base(manager)
        {
            this.modelId = modelId;
            this.animType = animType;
        }

        protected override void Load0()
        {
            var dictModel = Table.Blo.TableSailorModelBlo.GetTableSailorModel(modelId);
            if (dictModel != null)
            {
                modelRes = dictModel.ModelRes;
                isMainModel = dictModel.IsMain > 0;
                manager.StartCoroutine(_Load(modelId, loadTool));
            }
            else
            {
                loadResult = LoadEventState.Error;
            }
        }

        IEnumerator _Load(int modelId, LoadTools loadTool)
        {
            int modelLoadResult = 0;
            int animLoadResult = 0;
            int counter = 0;
            modelPath = AssetLoadHelper.modelPrefix + modelRes + "_l/" + modelRes + "_l_model";
            manager.StartCoroutine(AssetLoadHelper.Instance.LoadBundleObjectAsync(
                modelPath,
                (path) =>
                {
                    loadPathCache.Add(path);
                    loadTool.AddTask(path, Yunchang.AssetType.GameObject);
                    counter++;
                },
                (path) =>
                {
                    loadTool.CompleteTask(path);
                    counter--;
                    modelLoadResult = 1;
                }, AssetType.GameObject,
                (path) => {
                    loadTool.CompleteTask(path);
                    counter--;
                    modelLoadResult = -1;
                }
                ));
            modelAniPath = AssetLoadHelper.modelPrefix + modelRes + "/" + modelRes + (isMainModel ? "_" + animType : "_anim");
            manager.StartCoroutine(AssetLoadHelper.Instance.LoadBundleObjectAsync(
                modelAniPath,
                (path) =>
                {
                    loadPathCache.Add(path);
                    loadTool.AddTask(path, AssetType.AnimationController);
                    counter++;
                },
                (path) =>
                {
                    loadTool.CompleteTask(path);
                    counter--;
                    animLoadResult = 1;
                }, AssetType.AnimationController,
                (path) => {
                    loadTool.CompleteTask(path);
                    counter--;
                    animLoadResult = -1;
                }));
            while (counter > 0)
            {
                yield return null;
            }

            if (modelLoadResult == 1 && animLoadResult == 1)
            {
                loadResult = LoadEventState.Loaded;
            }
            else
            {
                loadResult = LoadEventState.Error;
            }
        }

        protected override void Unload0()
        {
            AssetLoadHelper.Instance.UnloadBundleObject(modelAniPath, AssetType.AnimationController);
            AssetLoadHelper.Instance.UnloadBundleObject(modelPath, AssetType.GameObject);
            loadTool.RemoveTask(modelPath);
            loadTool.RemoveTask(modelAniPath);
        }

        public GameObject SpawnModel()
        {
            if (IsLoadComplete())
            {
                GameObject model = AssetLoadHelper.Instance.SpawnBundleObject<GameObject>(
                    AssetLoadHelper.modelPrefix + modelRes + "/" + modelRes + "_model"
                    );
                if (model != null)
                {
                    var animator = AssetLoadHelper.Instance.SpawnBundleObject<RuntimeAnimatorController>(
                        AssetLoadHelper.modelPrefix + modelRes + "/" + modelRes + (isMainModel ? "_" + animType : "_anim"));
                    model.GetComponent<Animator>().runtimeAnimatorController = animator;
                }
                return model;
            }
            return null;
        }

        public void UnSpawnModel(GameObject model)
        {
            var ani = model.GetComponent<Animator>();
            if (ani != null)
            {
                ani.runtimeAnimatorController = null;
            }
            AssetLoadHelper.Instance.UnspawnBundleObject(model);
        }

        public int GetModelId()
        {
            return modelId;
        }

        public string GetAnimType()
        {
            return animType;
        }

        public override LoadEventType GetEventType()
        {
            return LoadEventType.MODEL;
        }

    }
}